<!DOCTYPE html>












  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">












<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">






















<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=6.4.1" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=6.4.1">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=6.4.1">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=6.4.1">


  <link rel="mask-icon" href="/images/logo.svg?v=6.4.1" color="#222">









<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '6.4.1',
    sidebar: {"position":"left","display":"hide","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta name="description" content="SQL长久以来是存储数据的主要方式，但NoSQL也吸引了越来越多的关注。它们有什么本质的区别？我们该如何选用它们？">
<meta name="keywords" content="数据库">
<meta property="og:type" content="article">
<meta property="og:title" content="SQL vs NoSQL">
<meta property="og:url" content="https://shaotao1988.github.io/2018/10/31/SQL-vs-NoSQL/index.html">
<meta property="og:site_name" content="Tao&#39;s Blog">
<meta property="og:description" content="SQL长久以来是存储数据的主要方式，但NoSQL也吸引了越来越多的关注。它们有什么本质的区别？我们该如何选用它们？">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2018-11-01T05:07:50.745Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="SQL vs NoSQL">
<meta name="twitter:description" content="SQL长久以来是存储数据的主要方式，但NoSQL也吸引了越来越多的关注。它们有什么本质的区别？我们该如何选用它们？">






  <link rel="canonical" href="https://shaotao1988.github.io/2018/10/31/SQL-vs-NoSQL/">



<script type="text/javascript" id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>SQL vs NoSQL | Tao's Blog</title>
  









  <noscript>
  <style type="text/css">
    .use-motion .motion-element,
    .use-motion .brand,
    .use-motion .menu-item,
    .sidebar-inner,
    .use-motion .post-block,
    .use-motion .pagination,
    .use-motion .comments,
    .use-motion .post-header,
    .use-motion .post-body,
    .use-motion .collection-title { opacity: initial; }

    .use-motion .logo,
    .use-motion .site-title,
    .use-motion .site-subtitle {
      opacity: initial;
      top: initial;
    }

    .use-motion {
      .logo-line-before i { left: initial; }
      .logo-line-after i { right: initial; }
    }
  </style>
</noscript>

</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Tao's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
    
  </div>

  <div class="site-nav-toggle">
    <button aria-label="Toggle navigation bar">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>



<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home">
    <a href="/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-home"></i> <br>Home</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-about">
    <a href="/about/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-user"></i> <br>About</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-tags">
    <a href="/tags/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>Tags</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">
    <a href="/archives/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>Archives</a>
  </li>

      
      
    </ul>
  

  
    

  

  
</nav>



  



</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://shaotao1988.github.io/2018/10/31/SQL-vs-NoSQL/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Shao Tao">
      <meta itemprop="description" content="Programming and Technical Sharing">
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Tao's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">SQL vs NoSQL
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              

              
                
              

              <time title="Created: 2018-10-31 00:00:00" itemprop="dateCreated datePublished" datetime="2018-10-31T00:00:00-05:00">2018-10-31</time>
            

            
              

              
                
                <span class="post-meta-divider">|</span>
                

                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                
                  <span class="post-meta-item-text">Edited on</span>
                
                <time title="Modified: 2018-11-01 00:07:50" itemprop="dateModified" datetime="2018-11-01T00:07:50-05:00">2018-11-01</time>
              
            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>SQL长久以来是存储数据的主要方式，但NoSQL也吸引了越来越多的关注。它们有什么本质的区别？我们该如何选用它们？<br><a id="more"></a></p>
<h1 id="SQL和NoSQL之间的战争"><a href="#SQL和NoSQL之间的战争" class="headerlink" title="SQL和NoSQL之间的战争"></a>SQL和NoSQL之间的战争</h1><p>首先澄清一些错误的观点。</p>
<h2 id="观点一：NoSQL会取代SQL"><a href="#观点一：NoSQL会取代SQL" class="headerlink" title="观点一：NoSQL会取代SQL"></a>观点一：NoSQL会取代SQL</h2><p>NoSQL和SQL做同样的事情：存储数据。它们使用不同的方法，可能帮助或者阻碍你的项目。</p>
<p>尽管NoSQL更新并且关注度越来越高，但它并不是SQL的替代品，而是为你提供另一种选择。</p>
<h2 id="观点二：-NoSQL比SQL好、或者坏"><a href="#观点二：-NoSQL比SQL好、或者坏" class="headerlink" title="观点二： NoSQL比SQL好、或者坏"></a>观点二： NoSQL比SQL好、或者坏</h2><p>有些项目更适合SQL，有些更适合NoSQL，而有些使用两者皆可。</p>
<h2 id="观点三：-SQL和NoSQL之间有明显的界限"><a href="#观点三：-SQL和NoSQL之间有明显的界限" class="headerlink" title="观点三： SQL和NoSQL之间有明显的界限"></a>观点三： SQL和NoSQL之间有明显的界限</h2><p>这并不一定正确。一些SQL正在吸取NoSQL的特性，反之亦然。它们之间的界限正变得越来越模糊，未来很可能出现一种NewSQL。</p>
<h1 id="SQL和NoSQL的主要区别"><a href="#SQL和NoSQL的主要区别" class="headerlink" title="SQL和NoSQL的主要区别"></a>SQL和NoSQL的主要区别</h1><h2 id="Tables-vs-Documents"><a href="#Tables-vs-Documents" class="headerlink" title="Tables vs Documents"></a>Tables vs Documents</h2><p>以在线商店储存书本为例，SQL以表格存储，比如：</p>
<table>
<thead>
<tr>
<th style="text-align:center">ISBN</th>
<th>Title</th>
<th>Author</th>
<th>Format</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">9567001</td>
<td>Introduction to Javascript</td>
<td>David</td>
<td>pdf</td>
<td>26.00</td>
</tr>
<tr>
<td style="text-align:center">9567002</td>
<td>Python Cookbook</td>
<td>Luis</td>
<td>pdf</td>
<td>28.00</td>
</tr>
</tbody>
</table>
<p>每一行都是不同书本的记录，无法使用这个表来存储不是书本的信息，并且每一列都有严格的数据类型。</p>
<p>NoSQL存储的是类似JSON的键值对，比如：<br><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">    ISBN: 9567001,</span><br><span class="line">    Title: "Introduction to Javascript",</span><br><span class="line">    Author: "David",</span><br><span class="line">    Format: "pdf",</span><br><span class="line">    Price: 26.00</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>你可以以任意类型存储任意数据到NoSQL数据库中。相比较而言，SQL表格定义了严格的数据模板，所以不容易犯错。而NoSQL更灵活，但是存储任意类型的数据容易导致解析时出错。</p>
<h2 id="SQL-Schema和NoSQL-Schemaless"><a href="#SQL-Schema和NoSQL-Schemaless" class="headerlink" title="SQL Schema和NoSQL Schemaless"></a>SQL Schema和NoSQL Schemaless</h2><p>SQL数据库在定义表格前无法存储任何数据，表格的格式称作Schema，包含主键、索引、关系、触发器和存储过程等。Schema必须在实现业务逻辑之前定义好，当业务上线后变更Schema会相当困难。</p>
<p>而在NoSQL数据库中，数据可以在任意地方、任意时间点添加，而无需事先定义存储数据的格式。比如下面的语句会创建名称为book的文档：<br><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">db.book.insert(</span><br><span class="line">    ISBN: <span class="number">9567001</span>,</span><br><span class="line">    Title: <span class="string">"Introduction to Javascript"</span>,</span><br><span class="line">    Author: <span class="string">"David"</span>,</span><br><span class="line">    Format: <span class="string">"pdf"</span>,</span><br><span class="line">    Price: <span class="number">26.00</span></span><br><span class="line">)</span><br></pre></td></tr></table></figure></p>
<p>NoSQL数据库可能更适合在项目初期数据格式很难确定的场景。但是如果忽略了在项目初期定义好正确的数据格式的重要性，在项目后期可能带来灾难。</p>
<h2 id="SQL-Normalization和NoSQL-DeNormalization"><a href="#SQL-Normalization和NoSQL-DeNormalization" class="headerlink" title="SQL Normalization和NoSQL DeNormalization"></a>SQL Normalization和NoSQL DeNormalization</h2><p>范式化保证无数据冗余，更新数据是更简单；反范式增加了数据冗余，在更新冗余数据是很麻烦，但是查询性能更好。</p>
<p>在SQL数据库中，为了获取更好的查询性能，对数据分片时，通常也会适当增加数据冗余来避免跨数据库join</p>
<h2 id="SQL-join-vs-NoSQL-scanning"><a href="#SQL-join-vs-NoSQL-scanning" class="headerlink" title="SQL join vs NoSQL scanning"></a>SQL join vs NoSQL scanning</h2><p>SQL的join语句可以从多个表中获取相关联的数据，而如果是NoSQL数据库，则必须遍历2个数据库并手动获取关联数据。</p>
<h2 id="数据完整性"><a href="#数据完整性" class="headerlink" title="数据完整性"></a>数据完整性</h2><p>SQL数据库使用外键对数据完整性加以约束，比如任意一本书籍都要包含出版商信息，同时当删除某个出版商的信息时，只要有任意一本该出版商出版的书籍存在数据库中，删除都无法成功。</p>
<p>NoSQL数据库没有这样的约束，NoSQL中的一条记录包含了关于这条记录的所有信息。</p>
<h2 id="事务"><a href="#事务" class="headerlink" title="事务"></a>事务</h2><p>SQL数据库具有很成熟的事务支持机制，而NoSQL只能保证单一document中的事务，跨document的事务则无法支持，只能由业务代码实现。</p>
<h2 id="性能"><a href="#性能" class="headerlink" title="性能"></a>性能</h2><p>由于不需要join和复杂的SQL语句，NoSQL在查询单条记录时性能更好。但是一个设计良好的SQL数据库的综合性能要好于一个设计不良的NoSQL数据库。</p>
<h2 id="扩展性"><a href="#扩展性" class="headerlink" title="扩展性"></a>扩展性</h2><p>NoSQL的反范式化，使之天生具有良好的扩展性。</p>
<h2 id="可用性"><a href="#可用性" class="headerlink" title="可用性"></a>可用性</h2><p>SQL数据库更成熟，被广泛使用和验证。而开发者和系统管理员对NoSQL的知识储备并不像SQL那么丰富，所以更容易犯错。</p>
<h1 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h1><p>SQL和NoSQL数据库用不同的方法做相同的事情。可以在开始时使用一种数据库，之后再切换为另一种，但是事先考虑和规划会节省更多的时间和金钱。</p>
<p>更适合SQL的场景：</p>
<ul>
<li>逻辑相关的离散数据，可以被预先定义和识别</li>
<li>事务和数据完整性非常重要</li>
<li>根据经验和被业界广泛证明了更适合SQL的场景</li>
</ul>
<p>更适合NoSQL的场景：</p>
<ul>
<li>不相关的、模糊的、逐步发展的数据</li>
<li>简单项目，可以马上启动编码</li>
<li>迫切要求速度和可扩展性的项目</li>
</ul>

      
    </div>

    

    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/数据库/" rel="tag"># 数据库</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/09/09/Redis-Best-Practice/" rel="next" title="Redis最佳实践">
                <i class="fa fa-chevron-left"></i> Redis最佳实践
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/12/20/分布式事务/" rel="prev" title="分布式事务">
                分布式事务 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>


  </div>


          </div>
          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            Overview
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/avatar.jpg" alt="Shao Tao">
            
              <p class="site-author-name" itemprop="name">Shao Tao</p>
              <p class="site-description motion-element" itemprop="description">Programming and Technical Sharing</p>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/archives/">
                
                    <span class="site-state-item-count">22</span>
                    <span class="site-state-item-name">posts</span>
                  </a>
                </div>
              

              

              
                
                
                <div class="site-state-item site-state-tags">
                  <a href="/tags/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">15</span>
                    <span class="site-state-item-name">tags</span>
                  </a>
                </div>
              
            </nav>
          

          

          

          
          

          
          

          
            
          
          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#SQL和NoSQL之间的战争"><span class="nav-number">1.</span> <span class="nav-text">SQL和NoSQL之间的战争</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#观点一：NoSQL会取代SQL"><span class="nav-number">1.1.</span> <span class="nav-text">观点一：NoSQL会取代SQL</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#观点二：-NoSQL比SQL好、或者坏"><span class="nav-number">1.2.</span> <span class="nav-text">观点二： NoSQL比SQL好、或者坏</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#观点三：-SQL和NoSQL之间有明显的界限"><span class="nav-number">1.3.</span> <span class="nav-text">观点三： SQL和NoSQL之间有明显的界限</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#SQL和NoSQL的主要区别"><span class="nav-number">2.</span> <span class="nav-text">SQL和NoSQL的主要区别</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#Tables-vs-Documents"><span class="nav-number">2.1.</span> <span class="nav-text">Tables vs Documents</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#SQL-Schema和NoSQL-Schemaless"><span class="nav-number">2.2.</span> <span class="nav-text">SQL Schema和NoSQL Schemaless</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#SQL-Normalization和NoSQL-DeNormalization"><span class="nav-number">2.3.</span> <span class="nav-text">SQL Normalization和NoSQL DeNormalization</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#SQL-join-vs-NoSQL-scanning"><span class="nav-number">2.4.</span> <span class="nav-text">SQL join vs NoSQL scanning</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#数据完整性"><span class="nav-number">2.5.</span> <span class="nav-text">数据完整性</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#事务"><span class="nav-number">2.6.</span> <span class="nav-text">事务</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#性能"><span class="nav-number">2.7.</span> <span class="nav-text">性能</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#扩展性"><span class="nav-number">2.8.</span> <span class="nav-text">扩展性</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#可用性"><span class="nav-number">2.9.</span> <span class="nav-text">可用性</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#总结"><span class="nav-number">3.</span> <span class="nav-text">总结</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Shao Tao</span>

  

  
</div>




  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> v3.8.0</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme – <a class="theme-link" target="_blank" href="https://theme-next.org">NexT.Mist</a> v6.4.1</div>




        








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    
	
    

    
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>


























  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=6.4.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=6.4.1"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=6.4.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=6.4.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=6.4.1"></script>



  



  










  





  

  

  

  

  
  

  

  

  

  

  

</body>
</html>
